/* Copyright (c) 2009-2010, Sean A.O. Harney <sharney3@gmail.com> */

#include "omf.h"


//returns leaf val (could be a list if first element of list was list)
EXPR_P builtin_func_hd(EXPR_P list)
{
    EXPR_P ret = NULL;
    LLIST_P llist;		//llist of evaled exprs.
    if (list->type != EXPR_LEAF || list->u.leaf.type != LEAF_LIST)
	fatal_error_println
	    ("OMF built-in function hd() expects LIST as argument.");

    llist = list->u.leaf.val.list_val;

    if (llist == NULL)		//empty list. NOTE: SML hd []; throws Empty exception
	fatal_error_println
	    ("OMF built-in function hd() used on empty list [].");

    ret = llist->element;	//first element of list.

//TODO: make new EXPR for it.
    return ret;
}
